import api from './request'

// 停车场管理API
export const parkingLotAPI = {
  // 获取停车场列表
  getList: (params: any) => api.get('/parking-lot/list', { params }),
  
  // 添加停车场
  add: (data: any) => api.post('/parking-lot/add', data),
  
  // 更新停车场
  update: (id: number, data: any) => api.put(`/parking-lot/update/${id}`, data),
  
  // 删除停车场
  delete: (id: number) => api.delete(`/parking-lot/delete/${id}`),
  
  // 获取停车场详情
  getDetail: (id: number) => api.get(`/parking-lot/detail/${id}`),
  
  // 获取停车场状态
  getStatus: (id: number) => api.get(`/parking-lot/status/${id}`),
  
  // 获取停车场统计
  getStatistics: (id: number) => api.get(`/parking-lot/statistics/${id}`)
}

// 车位管理API
export const parkingSpaceAPI = {
  // 获取车位列表
  getList: (params: any) => api.get('/parking-space/list', { params }),
  
  // 添加车位
  add: (data: any) => api.post('/parking-space/add', data),
  
  // 批量添加车位
  batchAdd: (data: any) => api.post('/parking-space/batch-add', data),
  
  // 更新车位
  update: (id: number, data: any) => api.put(`/parking-space/update/${id}`, data),
  
  // 删除车位
  delete: (id: number) => api.delete(`/parking-space/delete/${id}`),
  
  // 获取车位详情
  getDetail: (id: number) => api.get(`/parking-space/detail/${id}`),
  
  // 更新车位状态
  updateStatus: (id: number, status: number) => api.put(`/parking-space/status/${id}`, { status }),
  
  // 根据停车场ID获取车位列表
  getByLotId: (lotId: number) => api.get(`/parking-space/lot/${lotId}`),
  
  // 分配车位
  assign: (spaceId: number, userId: number) => api.post(`/parking-space/assign/${spaceId}`, { userId }),
  
  // 释放车位
  release: (spaceId: number) => api.post(`/parking-space/release/${spaceId}`)
}

// 充电桩管理API
export const chargingPileAPI = {
  // 获取充电桩列表
  getList: (params: any) => api.get('/charging-pile/list', { params }),
  
  // 添加充电桩
  add: (data: any) => api.post('/charging-pile/add', data),
  
  // 更新充电桩
  update: (id: number, data: any) => api.put(`/charging-pile/update/${id}`, data),
  
  // 删除充电桩
  delete: (id: number) => api.delete(`/charging-pile/delete/${id}`),
  
  // 获取充电桩详情
  getDetail: (id: number) => api.get(`/charging-pile/detail/${id}`),
  
  // 获取充电桩状态
  getStatus: (id: number) => api.get(`/charging-pile/status/${id}`),
  
  // 更新充电桩状态
  updateStatus: (id: number, status: number) => api.put(`/charging-pile/status/${id}`, { status }),
  
  // 根据停车场ID获取充电桩列表
  getByLotId: (lotId: number) => api.get(`/charging-pile/lot/${lotId}`),
  
  // 开始充电
  startCharging: (data: any) => api.post('/charging-pile/start-charging', data),
  
  // 停止充电
  stopCharging: (data: any) => api.post('/charging-pile/stop-charging', data)
}

// 充电桩分类管理API
export const chargingPileCategoryAPI = {
  // 获取充电桩分类列表
  getList: (params: any) => api.get('/charging-pile-category/list', { params }),
  
  // 根据类型获取充电桩分类列表
  getListByType: (categoryType: number) => api.get(`/charging-pile-category/list/by-type/${categoryType}`),
  
  // 获取启用的充电桩分类列表
  getEnabledList: () => api.get('/charging-pile-category/list/enabled'),
  
  // 根据功率获取适用的充电桩分类
  getListByPower: (power: number) => api.get('/charging-pile-category/list/by-power', { params: { power } }),
  
  // 创建充电桩分类
  create: (data: any) => api.post('/charging-pile-category/create', data),
  
  // 更新充电桩分类
  update: (id: number, data: any) => api.put(`/charging-pile-category/update/${id}`, data),
  
  // 删除充电桩分类
  delete: (id: number) => api.delete(`/charging-pile-category/delete/${id}`),
  
  // 获取充电桩分类详情
  getDetail: (id: number) => api.get(`/charging-pile-category/detail/${id}`),
  
  // 更新充电桩分类状态
  updateStatus: (id: number, status: number) => api.put(`/charging-pile-category/status/${id}`, { status }),
  
  // 根据功率自动判断充电桩分类
  getCategoryByPower: (power: number) => api.get('/charging-pile-category/auto-category', { params: { power } })
}

// 停车记录API
export const parkingRecordAPI = {
  // 获取停车记录列表
  getList: (params: any) => api.get('/parking-record/list', { params }),
  
  // 获取停车记录详情
  getDetail: (id: number) => api.get(`/parking-record/detail/${id}`),
  
  // 获取停车统计
  getStatistics: (params: any) => api.get('/parking-record/statistics', { params }),
  
  // 计算停车费用
  calculateFee: (recordId: number) => api.post(`/parking-record/calculate-fee?recordId=${recordId}`),
  
  // 车辆入场
  vehicleEntry: (lotId: number, licensePlate: string) => api.post('/parking-record/entry', { lotId, licensePlate }),
  
  // 车辆出场
  vehicleExit: (recordId: number) => api.post('/parking-record/exit', { recordId })
}

// 充电记录API
export const chargingRecordAPI = {
  // 获取充电记录列表
  getList: (params: any) => api.get('/charging-record/list', { params }),
  
  // 获取充电记录详情
  getDetail: (id: number) => api.get(`/charging-record/detail/${id}`),
  
  // 获取充电统计
  getStatistics: (params: any) => api.get('/charging-record/statistics', { params }),
  
  // 开始充电
  startCharging: (data: any) => api.post('/charging-record/start', data),
  
  // 停止充电
  stopCharging: (data: any) => api.post('/charging-record/stop', data),
  
  // 支付充电费用
  payCharging: (recordId: number, paymentMethod: string) => api.post('/charging-record/pay', { recordId, paymentMethod })
}

// 车辆管理API
export const vehicleAPI = {
  // 获取车辆列表
  getList: (params: any) => api.get('/vehicle/list', { params }),
  
  // 添加车辆
  add: (data: any) => api.post('/vehicle/add', data),
  
  // 更新车辆
  update: (id: number, data: any) => api.put(`/vehicle/update/${id}`, data),
  
  // 删除车辆
  delete: (id: number) => api.delete(`/vehicle/delete/${id}`),
  
  // 获取车辆详情
  getDetail: (id: number) => api.get(`/vehicle/detail/${id}`),
  
  // 获取我的车辆
  getMyVehicles: () => api.get('/vehicle/my-vehicles'),
  
  // 车辆认证
  verify: (data: any) => api.post('/vehicle/verify', data)
}

// 停车权限控制API
export const parkingPermissionAPI = {
  // 获取当前用户停车权限范围描述
  getCurrentUserScopeDescription: () => api.get('/parking-data-scope/scope-description'),
  
  // 获取指定用户停车权限范围描述
  getUserScopeDescription: (userId: number) => api.get(`/parking-data-scope/scope-description/${userId}`),
  
  // 获取当前用户可访问停车场ID列表
  getCurrentUserAccessibleParkingLots: () => api.get('/parking-data-scope/accessible-parking-lots'),
  
  // 获取指定用户可访问停车场ID列表
  getUserAccessibleParkingLots: (userId: number) => api.get(`/parking-data-scope/accessible-parking-lots/${userId}`),
  
  // 验证当前用户停车模块访问权限
  validateCurrentUserAccess: () => api.get('/parking-data-scope/validate-access'),
  
  // 验证指定用户停车模块访问权限
  validateUserAccess: (userId: number) => api.get(`/parking-data-scope/validate-access/${userId}`),
  
  // 获取当前用户停车权限缓存状态
  getCurrentUserCacheStatus: () => api.get('/parking-data-scope/cache-status'),
  
  // 获取指定用户停车权限缓存状态
  getUserCacheStatus: (userId: number) => api.get(`/parking-data-scope/cache-status/${userId}`),
  
  // 清除当前用户停车权限缓存
  clearCurrentUserCache: () => api.delete('/parking-data-scope/cache'),
  
  // 清除指定用户停车权限缓存
  clearUserCache: (userId: number) => api.delete(`/parking-data-scope/cache/${userId}`),
  
  // 刷新当前用户停车权限缓存
  refreshCurrentUserCache: () => api.post('/parking-data-scope/cache/refresh'),
  
  // 刷新指定用户停车权限缓存
  refreshUserCache: (userId: number) => api.post(`/parking-data-scope/cache/refresh/${userId}`),
  
  // 检查当前用户是否有指定停车场权限
  checkCurrentUserParkingLotPermission: (parkingLotId: number) => 
    api.get(`/parking-data-scope/check-parking-lot-permission/${parkingLotId}`),
  
  // 检查指定用户是否有指定停车场权限
  checkUserParkingLotPermission: (userId: number, parkingLotId: number) => 
    api.get(`/parking-data-scope/check-parking-lot-permission/${userId}/${parkingLotId}`)
}

// 数据权限管理API - 新增数据权限控制接口
export const dataScopeApi = {
  // 获取当前用户数据权限信息
  getDataScopeInfo: () => {
    return api.get<{
      code: number
      data: {
        userId: number
        isSuperAdmin: boolean
        isPropertyCompanyUser: boolean
        isOwnerUser: boolean
        communityIds: number[]
        propertyCompanyIds: number[]
        restrictions: string
        isSuperAdmin: boolean
        scopeDescription: string
      }
    }>('/api/parking/data-scope/current-user')
  },

  // 检查用户是否有指定小区权限
  checkCommunityPermission: (communityId: number) => {
    return api.get<{
      code: number
      data: { hasPermission: boolean }
    }>(`/api/parking/data-scope/community/${communityId}/check`)
  },

  // 批量检查用户小区权限
  batchCheckCommunityPermissions: (communityIds: number[]) => {
    return api.post<{
      code: number
      data: { authorizedCommunityIds: number[] }
    }>('/api/parking/data-scope/community/batch-check', {
      communityIds
    })
  },

  // 获取用户可访问的小区列表
  getUserCommunities: () => {
    return api.get<{
      code: number
      data: number[]
    }>('/api/parking/data-scope/user-communities')
  },

  // 刷新用户权限缓存
  refreshUserPermissionCache: () => {
    return api.post<{
      code: number
      data: { success: boolean }
    }>('/api/parking/data-scope/refresh-cache')
  },

  // 清除用户权限缓存
  clearUserPermissionCache: (userId?: number) => {
    return api.delete<{
      code: number
      data: { success: boolean }
    }>('/api/parking/data-scope/clear-cache', {
      params: { userId }
    })
  }
}

// 导出所有API
export default {
  parkingLot: parkingLotAPI,
  parkingSpace: parkingSpaceAPI,
  chargingPile: chargingPileAPI,
  parkingRecord: parkingRecordAPI,
  chargingRecord: chargingRecordAPI,
  vehicle: vehicleAPI,
  parkingPermission: parkingPermissionAPI,
  dataScope: dataScopeApi
} 